from Bases import BaseObject
from baseview import BaseView
from ..bases import widgets
from .. import editors
from ..childgroup_list import ChildGroupList

class PatchView(BaseView):
    topwidget_name = 'Patch Editor'
    topwidget_cls = widgets.VPane
    def __init__(self, **kwargs):
        self.topwidget = widgets.VBox()
        super(PatchView, self).__init__(**kwargs)
        self.DeviceSystem = self.MainController.DeviceSystem
        self.patch_editor = None
        
        col_attrs = ['id', 'manufacturer', 'name', 'personality_mode']
        col_names = [s.replace('_', ' ').title() for s in col_attrs]
        list_types = [str] * 4
        dlkwargs = dict(name='Definitions', 
                        child_group=self.DeviceSystem.DefinedDevices, 
                        col_attrs=col_attrs, 
                        col_names=col_names, 
                        list_types=list_types)
        self.device_list = self.add_editor(ChildGroupList, **dlkwargs)
        self.device_list.bind(current_child=self.on_device_list_current_child_set)
        
        self.topcontainer.pack_start(self.device_list.topwidget, expand=True)
        
        self.topwidget.show_all()
        
    def set_current_device(self, obj):
        self.remove_editor('patch_editor')
        if not len(self.DeviceSystem.universes):
            return
        self.add_editor(editors.PatchEditor, 'patch_editor', definition=obj)
        self.topcontainer.pack_start(self.patch_editor.topwidget)
        self.topcontainer.normalized_pos = .4
            
    def on_device_list_current_child_set(self, **kwargs):
        device = kwargs.get('value')
        self.set_current_device(device)

UI_VIEW = PatchView
